package top.sdaily.core.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import top.sdaily.core.mybatis.BaseMapper;
import top.sdaily.core.mybatis.Conditions;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;

/**
 * Created by soya on 2017/2/15.
 */
public abstract class BaseServiceImpl<T> implements BaseService<T> {

    @Autowired
    BaseMapper<T> baseMapper;

    private Class entityClazz;

    public BaseServiceImpl () {
        super();
        Type[] actualTypeArguments = ((ParameterizedType) this.getClass().getGenericSuperclass()).getActualTypeArguments();
        if (actualTypeArguments != null && actualTypeArguments.length > 0) {
            this.entityClazz = (Class) actualTypeArguments[0];
        }
    }

    public Conditions<T> condition () {
        return new Conditions<T>(this.entityClazz);
    }

    @Override
    public List findList(Conditions<T> conditions) {
        return baseMapper.findList(conditions);
    }

    @Override
    public T findOne(Conditions<T> conditions) {
        return baseMapper.findOne(conditions);
    }

    @Override
    public int insert(T entity) {
        return baseMapper.insert(entity);
    }

    @Override
    public int insertBatch(List<T> entity) {
        return baseMapper.insertBatch(entity);
    }

    @Override
    public int update(T entity) {
        return baseMapper.update(entity);
    }

    @Override
    public int delete(Conditions<T> conditions) {
        return baseMapper.delete(conditions);
    }

    @Override
    public T findById(String id) {
        return baseMapper.findOne(condition().id(id));
    }

    @Override
    public int delete(String id) {
        return baseMapper.delete(condition().id(id));
    }
}
